import { Game, GameComponent } from "@sapi-game/main";
import { PKBuildState } from "../buildState";

export class PKSkipBuildComponent extends GameComponent<PKBuildState> {
    private skipState = { blue: false, red: false };

    override onAttach(): void {
        this.subscribe(
            Game.events.itemUse,
            (t) => {
                const ans = this.context.groupSet.findById(t.source.id);
                if (!ans) return;

                const team =
                    ans.group === this.context.blue.team
                        ? "blue"
                        : ans.group === this.context.red.team
                        ? "red"
                        : null;

                if (!team) return;

                if (!this.skipState[team]) {
                    this.skipState[team] = true;
                    this.skipMes(team);
                } else {
                    t.source.sendMessage("无法再次跳过");
                }
                if (this.skipState.blue && this.skipState.red) {
                    this.state.toNextState();
                }
            },
            { itemId: "minecraft:clock" }
        );
    }

    private skipMes(team: keyof typeof this.skipState) {
        const teamName = team === "blue" ? "§b蓝队" : "§c红队";
        this.context.groupSet.forEach((p) => {
            p.sendMessage(`${teamName}已决定跳过建造阶段`);
        });
    }
}
